//
//  XHStatus.swift
//  gu-新浪微博
//
//  Created by xiaohui on 16/6/28.
//  Copyright © 2016年 gusheghui. All rights reserved.
//

import UIKit

class XHStatus: NSObject {

    var created_at: String?
    var id :Int64 = 0
    var text : String?
    var source : String?
    
    //用户信息
    var user: XHUser?
    
    var retweeted_status:XHStatus?
    
//    reposts_count	int	转发数
//    comments_count	int	评论数
//    attitudes_count	int	表态数

    var reposts_count :Int = 0{
        didSet{
            reposts_countStr = getCount(reposts_count, titlt: "转发")
        }
    }
    var comments_count :Int = 0{
        didSet{
            comments_countStr = getCount(comments_count, titlt: "评论")
        }
    }
    var attitudes_count :Int = 0{
        didSet{
            attitudes_countStr = getCount(attitudes_count, titlt: "赞")
        }
    }
    
    
    var reposts_countStr :String?
    var comments_countStr :String?
    var attitudes_countStr :String?
    
    //原创微博的图片url
    var pic_urls:[XHPictureInfo]?
    
    
    
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user"{
            guard let v = value as?[String:AnyObject] else{
                
                print("不能转模型")
                return
            }
            let u = XHUser(dict:v)
            //给转模型user辅助
            user = u
        }else if key == "retweeted_status"{
            guard let r = value as?[String:AnyObject] else{
                return
            }
            let s = XHStatus(dict: r)
            retweeted_status = s
        }else if key == "pic_urls" {
            guard let v = value as?[[String:AnyObject]] else{
                return
            }
            var tempArray:[XHPictureInfo]? = [XHPictureInfo]()
            for dic in v{
                let p = XHPictureInfo(dict: dic)
                tempArray?.append(p)
            }
            pic_urls = tempArray
        }else{
            super.setValue(value, forKey: key)
        }
        
        
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    //判断toolbar 显示的数据
    private func getCount(count:Int,titlt:String)->String{
        
        if count <= 0{
            return titlt
        }else if 0 < count && count<=9999 {
            return "\(count)"
        }else{
            let countF = CGFloat(count/1000)/10
            var countStr = "\(countF)"
            if countStr.containsString(".0"){
                countStr = countStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            
            return "\(countStr)万"
        }
    }
    
}
